/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <m-list-construction :title="'Master ' + $t('Manage')">
    <template slot="content">
      <div class="servers-wrapper" v-show="masterList.length">
        <div class="row-box" v-for="(item,$index) in masterList" :key="$index">
          <div class="row-title">
            <div class="left">
              <span class="sp">Host: {{item.host}}</span>
              <span class="sp">{{ $t('ZK') }}</span>
              <span class="ns">{{$t('Create Time')}}: {{item.createTime | formatDate}}</span>
              <span class="ns">{{$t('Last heartbeat time')}}: {{item.lastHeartbeatTime | formatDate}}</span>
              <!-- <span>{{$t('Zk registration directory')}} : <a href="javascript:" @click="_showZkDirectories(item)"
                   class="links">{{$t('Directory detail')}}</a></span> -->
            </div>
            <div class="right">
              <span>{{$t('Zk registration directory')}} : <a href="javascript:" @click="_showZkDirectories(item)"
                   class="links">{{$t('Directory detail')}}</a></span>
            </div>
            <!-- <div class="right">
              <span class="sp">{{$t('Create Time')}}: {{item.createTime | formatDate}}</span>
              <span class="sp">{{$t('Last heartbeat time')}}: {{item.lastHeartbeatTime | formatDate}}</span>
            </div> -->
          </div>
          <!-- <div class="row-middle">
            <div class="right">
              <span class="sp">{{$t('Create Time')}}: {{item.createTime | formatDate}}</span>
              <span class="sp">{{$t('Last heartbeat time')}}: {{item.lastHeartbeatTime | formatDate}}</span>
            </div>
          </div> -->
          <div class="row-cont">
            <div class="row-cont-item">
              <ChartCard title="处理器使用率" type="cpu" :percentage="(item.resInfo.cpuUsage*100).toFixed(0)"></ChartCard>
            </div>
            <div class="row-cont-item">
              <ChartCard title="内存使用率"  type="memory" :percentage="(item.resInfo.memoryUsage*100).toFixed(0)"></ChartCard>
            </div>
            <div class="row-cont-item">
              <ChartCard title="平均负载"  type="average" :percentage="(item.resInfo.loadAverage*100).toFixed(0)"></ChartCard>
            </div>
            <div class="row-cont-item">
              <ChartCard title="物理内存占用率"  type="phy" :percentage="((item.resInfo.dataUsed/item.resInfo.dataTotal)*100).toFixed(0)"></ChartCard>
            </div>
            <!-- <div class="row-cont-item">
              <ChartCard title="可用内存大小(G)"  type="phyMemory" :storage="item.resInfo.availablePhysicalMemorySize"></ChartCard>
            </div> -->
            <div class="row-cont-item">
              <ChartCard title="服务器状态"  type="server" :serverStatus="item.resInfo.serverStatus"></ChartCard>
            </div>
            <!-- <div class="row-box">
              <div class="top">
                <span class="sp">内存使用率</span>
              </div>
              <div class="middle">
                <img src="../servers/image/内存使用率.svg" style="margin: 20px 26px;">
                <div class="middle-text"><span class="sp">0.07</span></div>
              </div>
              <div class="bottom">
                3
              </div>
            </div>
            <div class="row-box">
              <div class="top">
                <span class="sp">平均负载</span>
              </div>
              <div class="middle">
                <img src="../servers/image/平均负载.svg" style="margin: 20px 26px;">
                <div class="middle-text"><span class="sp">0.07</span></div>
              </div>
              <div class="bottom">
                3
              </div>
            </div>
            <div class="row-box">
              <div class="top">
                <span class="sp">物理内存占用率</span>
              </div>
              <div class="middle">
                <img src="../servers/image/物理内存占用率.svg" style="margin: 20px 26px;">
                <div class="middle-text"><span class="sp">0.07</span></div>
              </div>
              <div class="bottom">
                3
              </div>
            </div>
            <div class="row-box">
              <div class="top">
                <span class="sp">服务器状态</span>
                <span style=""><img src="../servers/image/提示.svg"></span>
              </div>
              <div class="middle">
                <img src="../servers/image/服务器状态.svg" style="margin: 20px 26px;">
                <div class="middle-text"><span class="sp">0.07</span></div>
              </div>
              <div class="bottom">
                3
              </div>
            </div> -->
            <!-- <div class="col-md-4">
              <m-gauge :value="(item.resInfo.cpuUsage * 100).toFixed(2)" :name="'cpuUsage'" :id="'gauge-cpu-' + item.id">
              </m-gauge>
            </div>
            <div class="col-md-4">
              <m-gauge :value="(item.resInfo.memoryUsage * 100).toFixed(2)" :name="'memoryUsage'"
                       :id="'gauge-memory-' + item.id">
              </m-gauge>
            </div>
            <div class="col-md-4">
              <div style="height:250px;line-height:250px;font-size:90px;text-align:center;">
                <strong style="color: #33317e;">{{item.resInfo.loadAverage > 0? item.resInfo.loadAverage.toFixed(2):0}}</strong>
              </div>
              <div style="font-size: 32px;color:#333333;text-align:center;">
                loadAverage
              </div>
            </div> -->
          </div>
        </div>
      </div>
      <el-drawer append-to-body :visible.sync="drawer" :with-header="false">
        <zookeeper-directories-popup :zkDirectories=zkDirectories></zookeeper-directories-popup>
      </el-drawer>
      <div v-if="!masterList.length">
        <m-no-data></m-no-data>
      </div>
      <m-spin :is-spin="isLoading"></m-spin>
    </template>
  </m-list-construction>
</template>
<script>
  import _ from 'lodash'
  import { mapActions } from 'vuex'
  import ChartCard from './_source/ChartCard'
  // import mGauge from './_source/gauge'
  import mSpin from '@/module/components/spin/spin'
  import mNoData from '@/module/components/noData/noData'
  import themeData from '@/module/echarts/themeData.json'
  import mListConstruction from '@/module/components/listConstruction/listConstruction'
  import zookeeperDirectoriesPopup from './_source/zookeeperDirectories'

  export default {
    name: 'servers-master',
    data () {
      return {
        isLoading: false,
        masterList: [],
        color: themeData.color,
        drawer: false,
        zkDirectories: []
      }
    },
    props: {},
    methods: {
      ...mapActions('monitor', ['getMasterData']),
      _showZkDirectories (item) {
        this.zkDirectories = [{ zkDirectory: item.zkDirectory }]
        this.drawer = true
      }
    },
    watch: {},
    created () {
    },
    mounted () {
      this.isLoading = true
      this.getMasterData().then(res => {
        this.masterList = _.map(res, (v, i) => {
          return _.assign(v, {
            id: v.host + '_' + v.id,
            resInfo: JSON.parse(v.resInfo)
          })
        })
        this.isLoading = false
        // console.log(this.masterList)
      }).catch(() => {
        this.isLoading = false
      })
    },
    // components: { mListConstruction, mSpin, mNoData, mGauge, zookeeperDirectoriesPopup }
    components: { mListConstruction, mSpin, mNoData, zookeeperDirectoriesPopup, ChartCard }
  }
</script>
<style lang="scss" rel="stylesheet/scss">
@import './servers';
</style>
